<?php


/***********************************************************************

mySQL Database Access Class

************************************************************************/
# Database Settings


class ps_DB {

	var $lid = 0;             	// Link ID for database connection
	var $qid = 0;			// Query ID for current query
	var $row;			// Current row in query result set
	var $record = array();	// Current row record data
	var $error = "";		// Error Message
	var $errno = "";		// Error Number



	// Connects to DB and returns DB lid
	// PRIVATE
	function connect() {

		if ($this->lid == 0) {
			$this->lid = mysqli_connect(DB_HOST,DB_USER,DB_PWD);

			if (!$this->lid) {
				$this->halt("connect(" . DB_HOST .")  failed.");
			}

			mysqli_query("SET NAMES utf8",$this->lid);

			if (!@mysqli_select_db(DB_NAME,$this->lid)) {
				$this->halt("Cannot connect to database ".DB_NAME);
				return 0;
			}
		}
		return $this->lid;
	}


	// Runs query and sets up the query id for the class.
	// PUBLIC
	function query($q) {

		if (empty($q))
		return 0;

		if (!$this->connect()) {
			return 0;
		}

		if ($this->qid) {
			@mysqli_free_result($this->qid);
			$this->qid = 0;
		}

		$this->qid = @mysqli_query($q, $this->lid);
		$this->row   = 0;
		$this->errno = mysqli_errno();
		$this->error = mysqli_error();
		if (!$this->qid) {
			$this->halt("Invalid SQL: ".$q);
		}

		return $this->qid;
	}


	// Return next record in result set
	// PUBLIC
	function next_record() {

		if (!$this->qid) {
			$this->halt("next_record called with no query pending.");
			return 0;
		}

		$this->record = @mysqli_fetch_array($this->qid);
		$this->row   += 1;
		$this->errno  = mysqli_errno();
		$this->error  = mysqli_error();

		$stat = is_array($this->record);
		return $stat;
	}


	// Field Value
	// PUBLIC
	function f($field_name) {
		return stripslashes($this->record[$field_name]);
	}

	// Selective field value
	// PUBLIC
	function sf($field_name) {
		global $vars, $default;

		if ($vars["error"] and $vars["$field_name"]) {
			return stripslashes($vars["$field_name"]);
		} elseif ($default["$field_name"]) {
			return stripslashes($default["$field_name"]);
		} else {
			return stripslashes($this->record[$field_name]);
		}
	}

	// Print field
	// PUBLIC
	function p($field_name) {
		print stripslashes($this->record[$field_name]);
	}

	// Selective print field
	// PUBLIC
	function sp($field_name) {
		global $vars, $default;

		if ($vars["error"] and $vars["$field_name"]) {
			print stripslashes($vars["$field_name"]);
		} elseif ($default["$field_name"]) {
			print stripslashes($default["$field_name"]);
		} else {
			print stripslashes($this->record[$field_name]);
		}
	}

	// Returns the number of rows in query
	function num_rows() {

		if ($this->lid) {
			return @mysqli_numrows($this->qid);
		}
		else {
			return 0;
		}
	}





	// Halt and display error message
	// PRIVATE
	function halt($msg) {
		$this->error = @mysqli_error($this->lid);
		$this->errno = @mysqli_errno($this->lid);

		printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
		printf("<b>MySQL Error</b>: %s (%s)<br>\n",
		$this->errno,
		$this->error);

		exit;

	}

}
?>